package com.xdap.init.service.impl;

import com.xdap.init.service.InitPackageService;
import com.xdap.common.utils.GsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class InitPackageServiceImpl implements InitPackageService {


    @Override
    public String InitPackageService(String filePath, String packageName) {

        List<String> result = new ArrayList<>();

        //反斜杠在Java中需要使用转义字符
        filePath += "\\" + packageName;
        // 在此目录下创建文件夹
        File directory = new File(filePath);

        //mkdirs  -- 判断有没有这个文件夹，没有的话创建这个文件夹，以及父级文件夹
        boolean hasSucceeded = directory.mkdirs();

        log.info("创建{},结果：{}", filePath, hasSucceeded);
        result.add("创建" + filePath + ",结果：" + hasSucceeded);
        //mkdirs

        // 文件夹组成格式："D:\\data111\\test";

        // root目录已经创建成功，准备开始创建实际文件目录
        List<String> newPackagePathList = new ArrayList<>();
        newPackagePathList.add(filePath + "\\controller");
        newPackagePathList.add(filePath + "\\config");
        newPackagePathList.add(filePath + "\\dao");
        newPackagePathList.add(filePath + "\\pojo\\dto");
        newPackagePathList.add(filePath + "\\pojo\\vo");
        newPackagePathList.add(filePath + "\\pojo\\po");
        newPackagePathList.add(filePath + "\\service\\impl");
        newPackagePathList.add(filePath + "\\unit");

        for (String newPackagePath : newPackagePathList) {
            File newPackageDirectory = new File(newPackagePath);
            boolean newHashSucceeded = newPackageDirectory.mkdirs();
            log.info("创建{},结果：{}", newPackageDirectory, newHashSucceeded);
            result.add("创建" + newPackageDirectory + ",结果：" + newHashSucceeded);

        }
        return GsonUtil.toJson(result);
    }
}
